package coms.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import coms.datatransfer.FileItemDTO;
import coms.datatransfer.ReviewDTO;
import coms.datatransfer.UserDTO;
import coms.ejb.sessionbean.FileBeanRemote;
import coms.ejb.sessionbean.ReviewBeanRemote;
import coms.service.BusinessDelegate;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

public class ReviewAction extends ActionSupport implements ModelDriven<ReviewDTO>{
	
	private static final long serialVersionUID = 1L;
	private String fileId;
	private String userId;
	
	private BusinessDelegate delegate;
	private ReviewBeanRemote reviewBean = null;
	private FileBeanRemote fileBean = null;
	
	private List<ReviewDTO> reviewList = new ArrayList<ReviewDTO>();
	private List<FileItemDTO> reviewedFileList = new ArrayList<FileItemDTO>();
	private List<FileItemDTO> assignedList = new ArrayList<FileItemDTO>();
	
	private FileItemDTO file = new FileItemDTO();
	private ReviewDTO reviewDetail = new ReviewDTO();
	
	private String introduction;
	private String objective;
	private String method;
	private String result;
	private String conclusion;
	
	private String interest;
	private String important;
	private String novel;
	
	private String self;
	private String coherent;
	
	private String commentToAuthor;
	private String commentToAdmin;
	
	public ReviewAction(){
		delegate = new BusinessDelegate();
		reviewBean = (ReviewBeanRemote)delegate.getBean("reviewBean");
		fileBean = (FileBeanRemote)delegate.getBean("fileBean");
	}
	
	public List<FileItemDTO> getReviewedFileList() {return reviewedFileList;}

	public void setReviewedFileList(List<FileItemDTO> reviewedFileList) {this.reviewedFileList = reviewedFileList;}	

	public List<FileItemDTO> getAssignedList() {return assignedList;}

	public void setAssignedList(List<FileItemDTO> assignedList) {this.assignedList = assignedList;}

	public List<ReviewDTO> getReviewList() {return reviewList;}

	public void setReviewList(List<ReviewDTO> reviewList) {this.reviewList = reviewList;}	
	
	public void setFileId(String fileId) {this.fileId = fileId;}
    
    public String getFileId(){return this.fileId;}
   
    public void setUserId(String userId){this.userId = userId;}
    
    public String getUserId(){return this.userId;}
    
    public FileItemDTO getFile(){return this.file;}
    
    public void setFile(FileItemDTO file){this.file = file;}
    
    public void setReviewDetail(ReviewDTO reviewDetail){this.reviewDetail = reviewDetail;}
    
    public ReviewDTO getReviewDetail(){return this.reviewDetail;}
    
    //Quality of Research (25 points)
    public String getIntroduction(){return this.introduction;}
    public void setIntroduction(String introduction){this.introduction = introduction;}
    
    public String getObjective(){return this.objective;}
    public void setObjective(String objective){this.objective = objective;}
    
    public String getMethod(){return this.method;}
    public void setMethod(String method){this.method = method;}
    
    public String getResult(){return this.result;}
    public void setResult(String result){this.result = result;}
    
    public String getConclusion(){return this.conclusion;}
    public void setConclusion(String conclusion){this.conclusion = conclusion;}
    
    //Educational Value (15 points)
    public String getInterest(){return this.interest;}
    public void setInterest(String interest){this.interest = interest;}
    
    public String getImportant(){return this.important;}
    public void setImportant(String important){this.important = important;}
    
    public String getNovel(){return this.novel;}
    public void setNovel(String novel){this.novel = novel;}
    
    //Quality of Written Content (10 points)
    public String getSelf(){return this.self;}
    public void setSelf(String self){this.self = self;}
    
    public String getCoherent(){return this.coherent;}
    public void setCoherent(String coherent){this.coherent = coherent;}
    
    public String getCommentToAuthor(){return this.commentToAuthor;}
    public void setCommentToAuthor(String commentToAuthor){this.commentToAuthor = commentToAuthor;}
    
    public String getCommentToAdmin(){return this.commentToAdmin;}
    public void setCommentToAdmin(String commentToAdmin){this.commentToAdmin = commentToAdmin;}
    
	public String execute() throws Exception {
		System.out.println("inside action" + fileId);
		
		if(fileId.length() != 0){
			List<ReviewDTO> reviewDTOList = (List<ReviewDTO>)reviewBean.findByFileId(Integer.parseInt(fileId));
			System.out.println("String is: " + reviewDTOList.size());
		}
		
		if(userId.length() != 0){
			List<FileItemDTO> assignedList = (List<FileItemDTO>)reviewBean.displayAssignedFiles(Integer.parseInt(userId));
			System.out.println("List of Assigned: " + assignedList.size());
			
			List<ReviewDTO> reviewedList = (List<ReviewDTO>)reviewBean.displayReviewedFiles(Integer.parseInt(userId));
			System.out.println("List of Reviewed: " + reviewedList.size());
		}
		return "success";
	}
	
	public String displayAll() {
		try{
		System.out.println("calling display all");
		
		reviewList =(List<ReviewDTO>) reviewBean.displayReviewedFiles(5);
		assignedList = (List<FileItemDTO>)reviewBean.displayAssignedFiles(5);
		reviewedFileList = (List<FileItemDTO>)fileBean.getFilesByReview(reviewList);
		
		System.out.println("calling display all");
		System.out.println("List of Reviewed: " + reviewList.size());
		System.out.println("List of assigned: " + assignedList.size());
		System.out.println("List of reviewedFileList: " + reviewedFileList.size());
		for (FileItemDTO f :reviewedFileList)
		{
			System.out.println("file id is"+ f.getFileID()+ "file desc" + f.getFileName());
		}
		//createFileItem();
		return "display";
		} catch (Exception e){
			return "exception";
		}
	}
	
	public String displayReviewedList()  {
		try{
		System.out.println("display Reviewed List: ");
		Map<String,Object> session = ActionContext.getContext().getSession();
		
		if(session.get("userID") != null){
			reviewList =(List<ReviewDTO>) reviewBean.displayReviewedFiles((Integer)session.get("userID"));
		}/*else{
			//test
			reviewList =(List<ReviewDTO>) reviewBean.displayReviewedFiles(2);
			session.put("userID", 2);
		}*/
		
		reviewedFileList = (List<FileItemDTO>)fileBean.getFilesByReview(reviewList);
		//assignedList = (List<FileItemDTO>)reviewBean.displayAssignedFiles(5);
		return "display";
		} catch (Exception e){
			return "exception";
		}
	}
	
     public String displayAssignedList() {
    	 try{
    	 Map<String,Object> session = ActionContext.getContext().getSession();
    	 
    	 if(session.get("userID") != null){
    		 assignedList = (List<FileItemDTO>)reviewBean.displayAssignedFiles((Integer)session.get("userID"));
		 }/*else{
 			//test
			 assignedList = (List<FileItemDTO>)reviewBean.displayAssignedFiles(2);
 			 session.put("userID", 2);
 		 }*/
    	 
		 //assignedList = (List<FileItemDTO>)reviewBean.displayAssignedFiles(2);
		 return "display";
    	 } catch (Exception e){
 			return "exception";
 		}
	}
	
	public String displayFileDetail() {
		try{
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		Map<String,Object> session = ActionContext.getContext().getSession();
		fileBean = (FileBeanRemote)delegate.getBean("fileBean");
		
		if(request.getParameter("fileID") != null){
			session.put("fileID", request.getParameter("fileID"));
			file = fileBean.findFileItemById(Integer.parseInt((String)session.get("fileID")));
		}
		
		return "detail";
		} catch (Exception e){
			return "exception";
		}
	}
	
	public String displayReviewByFileAndUser()  {
		try{
		System.out.println("display Review By File And User review: " + reviewDetail + "and file: " + file);
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		Map<String,Object> session = ActionContext.getContext().getSession();
		
		if(request.getParameter("fileID") != null){
			session.put("fileID", request.getParameter("fileID"));
			file = fileBean.findFileItemById(Integer.parseInt((String)session.get("fileID")));
			String fileID = (String)session.get("fileID");
			String userID = ((Integer)session.get("userID")).toString();
			reviewDetail = reviewBean.displayReviewByFileAndUser(Integer.parseInt(fileID), Integer.parseInt(userID));
			System.out.println("review : " + reviewDetail);
		}
		return "success";
		} catch (Exception e){
			return "exception";
		}
	}
	
	public String updateReview() {
		try{
		System.out.println("update Review user id: " + reviewDetail.getReviewerID());
		if(reviewDetail.getReviewerID() == null){
			Map<String,Object> session = ActionContext.getContext().getSession();
			String userID = ((Integer)session.get("userID")).toString();
			String fileID = (String)session.get("fileID");
			
			reviewDetail.setReviewerID(Integer.parseInt(userID));
			reviewDetail.setFileID(Integer.parseInt(fileID));
			//reviewDetail.setMarks(Integer.toString(calculateQualityOfResearch() + calculateEducationalValue() + calculateWrittenContent()));
			/*reviewDetail.setCommentToAuthor(commentToAuthor);
			reviewDetail.setCommentToAdmin(commentToAdmin);*/
			//reviewBean.createOrUpdateReview(reviewDetail);
		}		
		reviewDetail.setMarks(Integer.toString(calculateQualityOfResearch() + calculateEducationalValue() + calculateWrittenContent()));		
		reviewBean.createOrUpdateReview(reviewDetail);
		
		return "success";		
		} catch (Exception e){
			return "exception";
		}
	}
		
	private int calculateQualityOfResearch(){
		System.out.println("calculate Quality of Research");
		int qualityOfResearch = 0;
		
		int introductionMark = 0;
		int objectiveMark = 0;
		int methodMark = 0;
		int resultMark = 0;
		int conclusionMark = 0;	
		
		if(introduction.equalsIgnoreCase("unacceptable")){
			introductionMark = 1;
		}else if(introduction.equalsIgnoreCase("marginal")){
			introductionMark = 2;
		}else if(introduction.equalsIgnoreCase("acceptable")){
			introductionMark = 3;
		}else if(introduction.equalsIgnoreCase("good")){
			introductionMark = 4;
		}else{
			introductionMark = 5;
		}
		
		if(objective.equalsIgnoreCase("unacceptable")){
			objectiveMark = 1;
		}else if(objective.equalsIgnoreCase("marginal")){
			objectiveMark = 2;
		}else if(objective.equalsIgnoreCase("acceptable")){
			objectiveMark = 3;
		}else if(objective.equalsIgnoreCase("good")){
			objectiveMark = 4;
		}else{
			objectiveMark = 5;
		}
		
		if(method.equalsIgnoreCase("unacceptable")){
			methodMark = 1;
		}else if(method.equalsIgnoreCase("marginal")){
			methodMark = 2;
		}else if(method.equalsIgnoreCase("acceptable")){
			methodMark = 3;
		}else if(method.equalsIgnoreCase("good")){
			methodMark = 4;
		}else{
			methodMark = 5;
		}
		
		if(result.equalsIgnoreCase("unacceptable")){
			resultMark = 1;
		}else if(method.equalsIgnoreCase("marginal")){
			resultMark = 2;
		}else if(method.equalsIgnoreCase("acceptable")){
			resultMark = 3;
		}else if(method.equalsIgnoreCase("good")){
			resultMark = 4;
		}else{
			resultMark = 5;
		}
		
		if(conclusion.equalsIgnoreCase("unacceptable")){
			conclusionMark = 1;
		}else if(conclusion.equalsIgnoreCase("marginal")){
			conclusionMark = 2;
		}else if(conclusion.equalsIgnoreCase("acceptable")){
			conclusionMark = 3;
		}else if(conclusion.equalsIgnoreCase("good")){
			conclusionMark = 4;
		}else{
			conclusionMark = 5;
		}
		
		qualityOfResearch = introductionMark + objectiveMark + methodMark + resultMark + conclusionMark;
		
		return qualityOfResearch;
	}
	
	private int calculateEducationalValue(){
		System.out.println("calculate Educational Value");
		System.out.println("interest = " + interest);
		int educationalValue = 0;
		
		int interestMark = 0;
		int importantMark = 0;
		int novelMark = 0;
		
		if(interest.equalsIgnoreCase("low")){
			interestMark = 1;
		}else if(interest.equalsIgnoreCase("semi-low")){
			interestMark = 2;
		}else if(interest.equalsIgnoreCase("moderate")){
			interestMark = 3;
		}else if(interest.equalsIgnoreCase("semi-high")){
			interestMark = 4;
		}else{
			interestMark = 5;
		}
		
		
		if(important.equalsIgnoreCase("low")){
			importantMark = 1;
		}else if(important.equalsIgnoreCase("semi-low")){
			importantMark = 2;
		}else if(important.equalsIgnoreCase("moderate")){
			importantMark = 3;
		}else if(important.equalsIgnoreCase("semi-high")){
			importantMark = 4;
		}else{
			importantMark = 5;
		}
		
		if(novel.equalsIgnoreCase("low")){
			novelMark = 1;
		}else if(novel.equalsIgnoreCase("semi-low")){
			novelMark = 2;
		}else if(novel.equalsIgnoreCase("moderate")){
			novelMark = 3;
		}else if(novel.equalsIgnoreCase("semi-high")){
			novelMark = 4;
		}else{
			novelMark = 5;
		}
		
		educationalValue = interestMark + importantMark + novelMark;
		
		return educationalValue;
	}
	
	private int calculateWrittenContent(){
		System.out.println("calculate Quality of Written Content");
		
		int writtenContent = 0;
		
		int selfMark = 0;
		int coherentMark = 0;
		
		if(self.equalsIgnoreCase("low")){
			selfMark = 1;
		}else if(self.equalsIgnoreCase("semi-low")){
			selfMark = 2;
		}else if(self.equalsIgnoreCase("moderate")){
			selfMark = 3;
		}else if(self.equalsIgnoreCase("semi-high")){
			selfMark = 4;
		}else{
			selfMark = 5;
		}
		
		
		if(coherent.equalsIgnoreCase("low")){
			coherentMark = 1;
		}else if(coherent.equalsIgnoreCase("semi-low")){
			coherentMark = 2;
		}else if(coherent.equalsIgnoreCase("moderate")){
			coherentMark = 3;
		}else if(coherent.equalsIgnoreCase("semi-high")){
			coherentMark = 4;
		}else{
			coherentMark = 5;
		}
		
		writtenContent = selfMark + coherentMark;
		
		return writtenContent;
	}
	
	public void createFileItem() throws Exception {
		System.out.println("calling create bean");
		
		FileItemDTO fileItemDTO = new FileItemDTO();
		fileItemDTO.setAuthorID(1);
		fileItemDTO.setCategory("newcat2");
		fileItemDTO.setFileName("file4");
		fileItemDTO.setLocation("filelocation");
		//fileItemDTO.setConferenceItemID(1);
		
		UserDTO userDTO1 = new UserDTO();
		userDTO1.setUserID(5);
		userDTO1.setFirstname("reviewer1FirstName");
		userDTO1.setLastname("reviewer1LastName");
		userDTO1.setProfile("userProfile");
		
		userDTO1.setPassword("gayathri");
		
		UserDTO userDTO2 = new UserDTO();
		userDTO2.setUserID(6);
		userDTO2.setFirstname("reviewer1FirstName");
		userDTO2.setLastname("reviewer1LastName");
		userDTO2.setProfile("userProfile");
		
		userDTO2.setPassword("gayathri");
		
		Set<UserDTO> listUser = new HashSet<UserDTO>();
		listUser.add(userDTO1);
		listUser.add(userDTO2);
		
		
		//FileItemDTO fileItemDTO = new FileItemDTO();
		//fileItemDTO.setFileID(4);
		fileItemDTO.setAuthorID(1);
		fileItemDTO.setCategory("newcat2");
		fileItemDTO.setFileName("file4");
		fileItemDTO.setLocation("filelocation");
		//fileItemDTO.setConferenceItemID(1);
		fileItemDTO.setListOfReviewers(listUser);
		
		fileBean.insertFileItem(fileItemDTO);
	}

	@Override
	public ReviewDTO getModel() {
		// TODO Auto-generated method stub
		return reviewDetail;
	}
	
}
